<template>
  <THeader title="填写信息">
    <template v-slot:icon>
      <span @click="sendSelectData">确定</span>
    </template>
  </THeader>

  <van-cell-group>
    <van-cell>
      <van-row style="border-left: 1vw solid #1989fa;">
        <van-col style="font-size: 4.5vw;padding-left: 1vw">上课信息</van-col>
      </van-row>
    </van-cell>
    <van-cell>
      <van-row class="van_row">
        <van-col span="5" offset="1" class="van_col"> 课程名称</van-col>
        <van-col span="18">{{ coursrName }}</van-col>
      </van-row>
      <van-row class="van_row">
        <van-col l span="5" offset="1" class="van_col"> 上课周次</van-col>
        <van-col span="18">{{ getiWeek(iWeek )}}</van-col>
      </van-row>
      <van-row class="van_row">
        <van-col l span="5" offset="1" class="van_col"> 上课星期</van-col>
        <van-col span="18">{{ weekDay }}</van-col>
      </van-row>
      <van-row class="van_row">
        <van-col span="5" offset="1" class="van_col"> 课程节次</van-col>
        <van-col span="18">{{ startSection }}</van-col>
      </van-row>

    </van-cell>
  </van-cell-group>

  <van-cell-group style="margin-top: 5vw">
    <van-cell>
      <van-row style="border-left: 1vw solid #1989fa;">
        <van-col style="font-size: 4.5vw;padding-left: 1vw">补课信息</van-col>
      </van-row>
    </van-cell>
  </van-cell-group>
  <div v-if="type==1">
    <van-cell-group class="buke">
      <!--      补课周次-->
      <van-field
          v-model="weekSelected"
          is-link
          readonly
          name="weekSelected"
          label="补课周次"
          placeholder="点击选择周次"
          @click="selectWeek = true"
      />
      <van-popup v-model:show="selectWeek" position="bottom">
        <van-picker
            :columns="weeks"
            :default-index="weekIndex"
            @confirm="onConfirmWeek"
            @cancel="selectWeek = false"
        />
      </van-popup>
      <!--      补课星期-->
      <van-field
          v-model="daySelected"
          is-link
          readonly
          name="daySelected"
          label="补课星期"
          placeholder="点击选择星期"
          @click="selectDay = true"
      />
      <van-popup v-model:show="selectDay" position="bottom">
        <van-picker
            :columns="days"
            :default-index="dayIndex"
            @confirm="onConfirmDay"
            @cancel="selectDay = false"
        />
      </van-popup>

      <!--      补课节次-->
      <van-field
          v-model="partSelected"
          is-link
          readonly
          name="partSelected"
          label="补课节次"
          placeholder="点击选择节次"
          @click="selectPart= true"
      />
      <van-popup v-model:show="selectPart" position="bottom">
        <van-picker
            :columns="parts"
            :default-index="partIndex"
            @confirm="onConfirmPart"
            @cancel="selectPart = false"
        />
      </van-popup>

      <!--      楼栋-->
      <van-field
          v-model="floorSelected"
          is-link
          readonly
          name="floorSelected"
          label="补课楼栋"
          placeholder="点击选择楼栋"
          @click="selectFloor= true"
      />
      <van-popup v-model:show="selectFloor" position="bottom">
        <van-picker
            :columns="floors"
            :default-index="floorIndex"
            @confirm="onConfirmFloor"
            @cancel="selectFloor = false"
        />
      </van-popup>

      <!--      教室-->
      <van-field
          v-model="roomSelected"
          is-link
          readonly
          name="roomSelected"
          label="补课教室"
          placeholder="点击选择教室"
          @click="handleRoomClick()"
      />
      <van-popup v-model:show="selectRoom" position="bottom">
        <van-picker
            :columns="rooms"
            :default-index="roomIndex"
            @confirm="onConfirmRoom"
            @cancel="selectRoom = false"

        />
      </van-popup>

    </van-cell-group>
  </div>
  <div v-else>

    <van-cell-group>


      <!--      学院-->
      <van-field
          v-model="academySelected"
          is-link
          readonly
          name="academySelected"
          label="学院"
          placeholder="点击选择学院"
          @click="selectAcademy= true"
      />
      <van-popup v-model:show="selectAcademy" position="bottom">
        <van-picker
            :columns="academys"
            :default-index="academyIndex"
            @confirm="onConfirmAcademy"
            @cancel="selectAcademy = false"
        />
      </van-popup>

      <!--      补课教师-->
      <van-field
          v-model="teacherSelected"
          is-link
          readonly
          name="teacherSelected"
          label="补课教师"
          placeholder="点击选择教师"
          @click="handleTeacherClick()"

      />
      <van-popup v-model:show="selectTeacher" position="bottom">
        <van-picker
            :columns="teachers"
            :default-index="teacherIndex"
            @confirm="onConfirmTeacher"
            @cancel="selectTeacher=false"
        />
      </van-popup>

    </van-cell-group>
  </div>
</template>
<script setup>
import {getCourse} from "/@/api/ttsq";
import {useFillStore} from "/@/store/modules/fillInfo";
import {Toast} from "vant";
import {getAllCollege, getTeacher} from "../../../../api/ttsq";

const fillStore = useFillStore()
const router = new useRouter()
const route = useRoute()

const coursrName = route.query.coursrName
const weekName = route.query.weekName
const weekDay = route.query.weekDay
const startSection = route.query.startSection
const classesName = route.query.classesName
const iWeek = route.query.iWeek
const start = route.query.start
const end = route.query.end
const type = route.query.type
const academy = route.query.academy
// console.log("academy",academy)
const roomNumber = route.query.roomNumber
const id = route.query.id
console.log("id", id)
console.log("iWeek", iWeek)
const weekSelected = ref(null)
const daySelected = ref(null)
const partSelected = ref(null)
const floorSelected = ref(null)
const roomSelected = ref(null)
const teacherSelected = ref(null)
const academySelected = ref(null)
//选中
const weekIndex = ref(null)
const dayIndex = ref(null)
const partIndex = ref(null)
const floorIndex = ref(null)
const roomIndex = ref(null)
const teacherIndex = ref(null)
const academyIndex = ref(null)
const selectWeek = ref(false)
const weeks = ref([])
// 周
const initWeeks = () => {
  weekSelected.value = '第' + iWeek + '周'
  // console.log("weekSelected", weekSelected.value)
  for (let i = 0; i < 20; i++) {
    let j = i + 1
    weeks.value.push('第' + j + '周');
    // console.log("weeks", weeks.value[i])
    if (weekSelected.value == weeks.value[i]) {
      weekIndex.value = i
      // console.log("weekIndex", weekIndex.value)
    }

  }

  // console.log(weeks.value)
}
const onConfirmWeek = (value) => {
  weekSelected.value = value;
  // console.log(value)
  selectWeek.value = false;
}
const getiWeek=(iweek)=>{
  return '第'+iweek+'周'
}
// 星期
const selectDay = ref(false)
const days = ref([])
const initDays = () => {
  daySelected.value = weekDay;
  let arr = ["一", "二", "三", "四", "五", "六", "日"]
  for (let arrKey in arr) {
    days.value.push('星期' + arr[arrKey])
    if (daySelected.value == days.value[arrKey])
      dayIndex.value = arrKey
  }

}
const onConfirmDay = (value) => {
  daySelected.value = value;
  // console.log(value)
  selectDay.value = false;
}
// 节
const selectPart = ref(false)
const parts = ref([])
const initParts = () => {
  partSelected.value = startSection
  let arr = ["1-2", "3-4", "5-6", "7-8", "9-10"]
  for (let arrKey in arr) {
    parts.value.push(arr[arrKey] + '节')
    if (partSelected.value == parts.value[arrKey])
      partIndex.value = arrKey
  }

}

const onConfirmPart = (value) => {
  partSelected.value = value;
  // console.log(value)
  selectPart.value = false;
}

// 楼栋
const building = ref([])
const selectFloor = ref(false)
const floors = ref([])

const initFloors = async () => {
  if (type == 1) {
    const regex = /\d+(?=[A-Z])/g;
    const match = roomNumber.match(regex);
    let str = match ? match[0] : ''
    if (str) {
      floorSelected.value = str + "号楼"
      roomSelected.value = roomNumber
    }
    const res = await getCourse(start, end, Number(type));
    building.value = res.response.value.extend.building
    console.log("buliding",building.value)
    // building.value.map((item) => {
    for (let buildingKey in building.value) {
      floors.value.push(building.value[buildingKey].name)
      if (building.value[buildingKey].name === floorSelected.value) {
        floorIndex.value = buildingKey
        for (const itemKey in building.value[buildingKey].rooms) {
          rooms.value.push(building.value[buildingKey].rooms[itemKey].name)
          if (roomSelected.value == rooms.value[itemKey])
            roomIndex.value = itemKey
        }
        // console.log("room",rooms.value)
      }
    }
  }
  // console.log("floors", floors.value)
}

const onConfirmFloor = (value) => {
  floorSelected.value = value;
  // console.log(value)
  selectFloor.value = false;
}


// 教室
const selectRoom = ref(false)
const rooms = ref([])
const onConfirmRoom = (value) => {
  roomSelected.value = value;
  // console.log(value)
  selectRoom.value = false;
}
// 监听楼栋
watch(() => floorSelected.value, () => {
  if (floorSelected.value) {
    rooms.value=[]
    console.log("newValueFloor", floorSelected.value)
    for (let buildingKey in building.value) {
      if (building.value[buildingKey].name === floorSelected.value) {
        let arr = building.value[buildingKey].rooms
        console.log("arr",arr)
        for (let arrKey in arr) {
          rooms.value.push(arr[arrKey].name)
        }
        console.log("rooms", rooms.value)
        roomSelected.value=rooms.value[0]
        break
      }
    }
  }
})

// 教师
const selectTeacher = ref(false)
const teachers = ref([])
const initTeachers = async (ac) => {
  // console.log("initTeachers2222222222222222222222222222222")
  let id = null
  if (academySelected.value) {
    for (let valueKey in all.value) {
      if (all.value[valueKey].name == ac) {
        id = all.value[valueKey].number
        break
      }
    }
    if (id) {
      const res = await getTeacher(id)
      // console.log("getTeacher", res.response.value.extend.teachers)
      let temp = res.response.value.extend.teachers
      teachers.value = []
      // console.log("temp", temp.length)
      for (let i in temp) {
        teachers.value.push(temp[i].name)
      }
    }

  }
}
const handleTeacherClick = () => {
  if (teachers.value && teachers.value.length > 0) {
    selectTeacher.value = true;
  }
}
const handleRoomClick = () => {
  if (rooms.value && rooms.value.length > 0) {
    selectRoom.value = true;
  }
}
const onConfirmTeacher = (value) => {
  teacherSelected.value = value
  selectTeacher.value = false
}
// 学院
const selectAcademy = ref()
const academys = ref([])
const all = ref([])
const initAcademys = async () => {
  if (type == 0) {
    const res = await getAllCollege()
    // console.log("AllCollege", res.response.value.extend.colleges)
    all.value = res.response.value.extend.colleges
    for (let tempKey in all.value) {
      academys.value.push(all.value[tempKey].name)

    }
    for (let academyKey in all.value) {
      if (all.value[academyKey].number == academy) {
        academySelected.value = all.value[academyKey].name
        academyIndex.value = academyKey
        // console.log("academySelected777777777777", academySelected.value)
        break
      }
    }
  }
}

const onConfirmAcademy = (value) => {
  academySelected.value = value
  selectAcademy.value = false
}
watch(() => academySelected.value,
    (newValue) => {
      initTeachers(newValue)
    })

// 数字转字符串
const getPart = (part) => {
  if (part === "1-2节") {
    return '1';
  } else if (part === "3-4节") {
    return '3';
  } else if (part === "5-6节") {
    return '5';
  } else if (part === "7-8节") {
    return '7';
  } else if (part === "9-10节") {
    return '9';
  }
}
// 字符串转数字
const getDay = (sendDay) => {
  if ("星期一" === sendDay) {
    return '1';
  } else if ("星期二" === sendDay) {
    return '2';
  } else if ("星期三" === sendDay) {
    return '3';
  } else if ("星期四" === sendDay) {
    return '4';
  } else if ("星期五" === sendDay) {
    return '5';
  } else if ("星期六" === sendDay) {
    return '6';
  } else if ("星期日" === sendDay) {
    return '7';
  }
}
const sendSelectData = () => {
  if (type == 1) {

    if (weekSelected.value &&
        daySelected.value &&
        partSelected.value &&
        floorSelected.value &&
        roomSelected.value) {

      // console.log("weekSelected", weekSelected.value)
      // console.log("daySelected", daySelected.value)
      // console.log("partSelected", partSelected.value)
      // console.log("floorSelected", floorSelected.value)
      // console.log("roomSelected", roomSelected.value)


      const regex = /第(\d+)周/;
      const match = weekSelected.value.match(regex);
      // console.log("啦啦啦", match[1])//周
      // console.log("啦啦啦", getDay(daySelected.value))//星期
      // console.log("啦啦啦", getPart(partSelected.value))//节

      // router.replace( '/stop')
      const data = {
        "id": id,
        "iWeek":iWeek,
        "weekSelected": match[1],
        "daySelected": getDay(daySelected.value),
        "partSelected": getPart(partSelected.value),
        "floorSelected": floorSelected.value,
        "roomSelected": roomSelected.value,
      }

      fillStore.setFillData(JSON.stringify(data))
      // console.log("(fillStore.fillData  ",JSON.parse(fillStore.fillData))
      // Toast("操作成功")
      router.back()
    } else {
      Toast("请完善信息")
    }
  } else {
    //调课
    if (academySelected.value && teacherSelected.value) {
      // console.log("academySelected", academySelected.value)
      // console.log("teacherSelected", teacherSelected.value)

      const data = {
        "id": id,
        "iWeek":iWeek,
        "academySelected": academySelected.value,
        "teacherSelected": teacherSelected.value,
      }
      // console.log("data", data)
      fillStore.setFillData(JSON.stringify(data))
      // console.log("(fillStore.fillData  ",JSON.parse(fillStore.fillData))

      // Toast("操作成功")
      router.back()
    } else {
      Toast("请完善信息")
    }

  }

}

onMounted(() => {
  initWeeks();
  initDays();
  initParts();
  initFloors();
  initAcademys();
  initTeachers();
})

</script>
<style scoped>
.van_row {
  text-align: center;
  font-size: 4vw;
  padding: 1.2vw;
}

.van_col {
  color: #9a9b9d;
}


.custom-col {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #9a9b9d;
}

.blank {
  width: 100%;
  height: 43vh;
}

.buke .van-cell {
  /*border: 1px solid red;*/
  /*font-size: 4vw;*/
  padding: 3vw;
}

>>> .van-field__label {
  text-align: center;
  font-size: 4vw;
  color: #9a9b9d;
}

>>> .van-field__control {
  font-size: 4vw;
  text-align: center;
}
</style>
